<?php
namespace App\Model;
use App\Repository\Sys\Db;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
//导航设置
class NavTable{
    use Db;
    static private $table = 'nav';
    //导航-列表
    static public function getNavList($status){
    	$page = 1;
        $limit = 100;
    	$offset = ($page - 1) * $limit;
        //拼接sql
        $where = "1=1";
        $parameters = array(
            'limit' => $limit,
            'offset' => $offset
        );
        if ($status) {
            $where .= " AND d.status = :status  AND d.depth = :depth ";
            $parameters['depth'] = 1;
            $parameters['status'] = $status;
        } else {
        	$where .= " AND d.status>0 AND d.depth = :depth ";
            $parameters['depth'] = 1;
        }
        $sql = "select * from nav as d  WHERE {$where} ORDER BY d.sort ASC,d.id ASC limit  :offset,:limit;";
        //执行sql
        $res = \DB::select($sql, $parameters);
        unset($parameters['limit'], $parameters['offset']);
        $count = \DB::select("select count(*) as c from nav as d WHERE {$where}", $parameters)[0]->c;
        $data=[];
        foreach ($res as $v) {
        	$v->status_text=ConstTable::STATUS_NAME[$v->status];
        	$id_category=$v->id_category;
        	$id=$v->id;
        	
        	$count_nav = \DB::select("select count(*) as c from nav as d WHERE status>0 AND id_category={$id}")[0]->c;
	        $v->count = $count_nav;
	        $nav = $db = NavTable::ORM()->where('id_category', $id)->orderBy('sort', 'asc')->get();
            $data_list=[];
            foreach ($nav as $vv) {
            	$vv->status_text=ConstTable::STATUS_NAME[$vv->status];
            	$data_list[] = $vv;
            }
            $v->nav_li=$data_list;
            
        	if ($status) {
            	if($count_nav>0){
		        	$v->url = 'javascript:;';
		        }
	        }
        	$data[] = $v;
        }
        if ($status) {
	        return JsonRespose::jsonArray(1,$data);
	    } else {
	        return JsonRespose::jsonArray(1,[
	            'count' => $count,
	            'limit' => $limit,
	            'page' => $page,
	            'list' => $data
	        ]);
	    }
    }
    //导航-详情
    static public function getNavInfo($id){
        if(!$id){
            return JsonRespose::jsonArray(1, []);
        }

    	//获取完整url
    	$http_url=$_SERVER['HTTP_REFERER'];
    	//解析url为数组格式
    	preg_match_all("/(\w+=\w+)(#\w+)?/i",$http_url,$url_arry);
    	//解析parse_url函数生成的 query部分
    	parse_str($url_arry[0][0]);
    	//var_dump('<br>--getNavInfo--<br>');
		//var_dump($id);
    	if(!$id){
    		//如果id不存在,id就等于url中解析的 page_id
    		$id = $page_id;
    	}
    	//查询当前页面,当前导航数据
        $res = NavTable::ORM()->where('id', $id)->first();
        //var_dump('<br>--$res--<br>');
		//var_dump($res);
        //获取下级分类的个数
		$count = \DB::select("select count(*) as c from nav as d WHERE id_category={$id}",[])[0]->c;
        $res->count = $count;
        //获取上级分类名称
	    $res_pans = $db = NavTable::ORM()->where('id',$res->id_category)->first();
        $res->name_category = $res_pans->name;
        if (!$res) {
            return JsonRespose::jsonArray(0, null, '数据不存在');
        }
        return JsonRespose::jsonArray(1, $res);
    }
}